<template>
  <!-- 生产线边库存表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="10%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="50px">
      <!--      <el-form-item label="工厂编号" prop="plantCode">-->
      <!--        <el-input-->
      <!--          v-if="!props.isReadonly"-->
      <!--          v-model="formData.plantCode"-->
      <!--          placeholder="请输入工厂编号"-->
      <!--          clearable-->
      <!--        />-->

      <!--        <span v-else>{{ formData.plantCode }}</span>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="物料编号" prop="materialCode">-->
      <!--        <el-input-->
      <!--          v-if="!props.isReadonly"-->
      <!--          v-model="formData.materialCode"-->
      <!--          placeholder="请输入物料编号"-->
      <!--          clearable-->
      <!--        />-->

      <!--        <span v-else>{{ formData.materialCode }}</span>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="库存来源" prop="stockFrom">-->
      <!--        <el-select-->
      <!--          v-if="!props.isReadonly"-->
      <!--          v-model="formData.stockFrom"-->
      <!--          placeholder="请选择库存来源"-->
      <!--          clearable-->
      <!--        >-->
      <!--          <el-option-->
      <!--            v-for="item in dictionaryStore.filterDictList('StockFrom')"-->
      <!--            :key="item.value"-->
      <!--            :label="item.label"-->
      <!--            :value="item.value"-->
      <!--          />-->
      <!--        </el-select>-->

      <!--        <span v-else>{{-->
      <!--          dictionaryStore.filterDictName("StockFrom", formData.stockFrom)-->
      <!--        }}</span>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="批次号" prop="materialBatch">-->
      <!--        <el-input-->
      <!--          v-if="!props.isReadonly"-->
      <!--          v-model="formData.materialBatch"-->
      <!--          placeholder="请输入批次号"-->
      <!--          clearable-->
      <!--        />-->

      <!--        <span v-else>{{ formData.materialBatch }}</span>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item-->
      <!--        label="物料条码;工序流转单号/仓库包装单位号"-->
      <!--        prop="materialBarcode"-->
      <!--      >-->
      <!--        <el-input-->
      <!--          v-if="!props.isReadonly"-->
      <!--          v-model="formData.materialBarcode"-->
      <!--          placeholder="请输入物料条码;工序流转单号/仓库包装单位号"-->
      <!--          clearable-->
      <!--        />-->

      <!--        <span v-else>{{ formData.materialBarcode }}</span>-->
      <!--      </el-form-item>-->
      <el-form-item label="数量" prop="materialQuantity">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.materialQuantity"
          placeholder="请输入数量"
          clearable
        />

        <span v-else>{{ formData.materialQuantity }}</span>
      </el-form-item>
      <!--      <el-form-item label="数量单位" prop="quantityUnit">-->
      <!--        <el-select-->
      <!--          v-if="!props.isReadonly"-->
      <!--          v-model="formData.quantityUnit"-->
      <!--          placeholder="请选择数量单位"-->
      <!--          clearable-->
      <!--        >-->
      <!--          <el-option-->
      <!--            v-for="item in dictionaryStore.filterDictList('MeasurementUnit')"-->
      <!--            :key="item.value"-->
      <!--            :label="item.label"-->
      <!--            :value="item.value"-->
      <!--          />-->
      <!--        </el-select>-->

      <!--        <span v-else>{{-->
      <!--          dictionaryStore.filterDictName(-->
      <!--            "MeasurementUnit",-->
      <!--            formData.quantityUnit-->
      <!--          )-->
      <!--        }}</span>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="是否合格" prop="isValid">-->
      <!--        <el-select-->
      <!--          v-if="!props.isReadonly"-->
      <!--          v-model="formData.isValid"-->
      <!--          placeholder="请选择是否合格"-->
      <!--          clearable-->
      <!--        >-->
      <!--          <el-option-->
      <!--            v-for="item in dictionaryStore.filterDictList('YesNo')"-->
      <!--            :key="item.value"-->
      <!--            :label="item.label"-->
      <!--            :value="item.value"-->
      <!--          />-->
      <!--        </el-select>-->

      <!--        <span v-else>{{-->
      <!--          dictionaryStore.filterDictName("YesNo", formData.isValid)-->
      <!--        }}</span>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="是否正在使用" prop="isUse">-->
      <!--        <el-select-->
      <!--          v-if="!props.isReadonly"-->
      <!--          v-model="formData.isUse"-->
      <!--          placeholder="请选择是否正在使用"-->
      <!--          clearable-->
      <!--        >-->
      <!--          <el-option-->
      <!--            v-for="item in dictionaryStore.filterDictList('YesNo')"-->
      <!--            :key="item.value"-->
      <!--            :label="item.label"-->
      <!--            :value="item.value"-->
      <!--          />-->
      <!--        </el-select>-->

      <!--        <span v-else>{{-->
      <!--          dictionaryStore.filterDictName("YesNo", formData.isUse)-->
      <!--        }}</span>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="工作中心编号" prop="workCenterCode">-->
      <!--        <el-input-->
      <!--          v-if="!props.isReadonly"-->
      <!--          v-model="formData.workCenterCode"-->
      <!--          placeholder="请输入工作中心编号"-->
      <!--          clearable-->
      <!--        />-->

      <!--        <span v-else>{{ formData.workCenterCode }}</span>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="备注" prop="remarks">-->
      <!--        <el-input-->
      <!--          v-if="!props.isReadonly"-->
      <!--          v-model="formData.remarks"-->
      <!--          placeholder="请输入备注"-->
      <!--          clearable-->
      <!--        />-->

      <!--        <span v-else>{{ formData.remarks }}</span>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="是否启用" prop="enabled">-->
      <!--        <el-input-->
      <!--          v-if="!props.isReadonly"-->
      <!--          v-model="formData.enabled"-->
      <!--          placeholder="请输入是否启用"-->
      <!--          clearable-->
      <!--        />-->

      <!--        <span v-else>{{ formData.enabled }}</span>-->
      <!--      </el-form-item>-->
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESMESPRODUCINGSTOCKADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  plantCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  stockFrom: [],
  materialBatch: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialBarcode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialQuantity: [],
  quantityUnit: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  isValid: [],
  isUse: [],
  workCenterCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  createTime: [],
  createdUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],
  lastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  enabled: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  organizationId: "",
  organizationCode: "",
  plantId: "",
  plantCode: "",
  materialId: "",
  materialCode: "",
  stockFrom: "",
  materialBatch: "",
  materialBarcode: "",
  materialQuantity: "",
  quantityUnit: "",
  isValid: "",
  isUse: "",
  workCenterId: "",
  workCenterCode: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        materialId,
        materialCode,
        stockFrom,
        materialBatch,
        materialBarcode,
        materialQuantity,
        quantityUnit,
        isValid,
        isUse,
        workCenterId,
        workCenterCode,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        materialId,
        materialCode,
        stockFrom,
        materialBatch,
        materialBarcode,
        materialQuantity,
        quantityUnit,
        isValid,
        isUse,
        workCenterId,
        workCenterCode,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.materialId = "";
  formData.materialCode = "";
  formData.stockFrom = "";
  formData.materialBatch = "";
  formData.materialBarcode = "";
  formData.materialQuantity = "";
  formData.quantityUnit = "";
  formData.isValid = "";
  formData.isUse = "";
  formData.workCenterId = "";
  formData.workCenterCode = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 生产线边库存保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mesproducingstock/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
